package com.example.downloadmvpdemo;

import android.util.Log;
import android.view.View;

import com.example.downloadmvpdemo.MainContract.DownloadUtil;
import com.example.downloadmvpdemo.MainContract.MainView;

public class PersenterImpl implements MainContract.Persenter, DownloadUtil.onDownloadListener {

    // Log tag
    private static final String LOG_TAG = "PersenterImpl";

    // MainView
    private MainView mainView = null;

    // downloadUtil
    private DownloadUtil downloadUtil = null;

    // flag
    private boolean isDownloading = false;

    public PersenterImpl(MainView mainView, DownloadUtil downloadUtil) {
        this.mainView     = mainView;
        this.downloadUtil = downloadUtil;
    }


    @Override
    public void onDownloadStart() {
        isDownloading = true;
        mainView.onImageViewShowDownloadStatus(StatusMsg.IMAGE_DOWNLOAD_STATUS_START);
    }

    @Override
    public void onDownloadPause(int progress) {
        isDownloading = false;
        mainView.onImageViewShowDownloadStatus(StatusMsg.IMAGE_DOWNLOAD_STATUS_STOP);
    }

    @Override
    public void onDownloadFinish() {
        isDownloading = false;
        mainView.onImageViewShowDownloadStatus(StatusMsg.IMAGE_DOWNLOAD_STATUS_FINISH);
        mainView.onDownloadFinished();
    }

    @Override
    public void onDownloadProgress(int progress) {
        mainView.onTextViewShowProgress(progress);
        mainView.onProgressBarStep(progress);
    }

    @Override
    public void onButtonClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
            case R.id.btn_goon:
                DEBUG("Start");
                if (!isDownloading) {
                    DEBUG("Start enter");
                    isDownloading = true;
                    downloadUtil.startDownload(this);
                }
                break;
            case R.id.btn_pause:
                DEBUG("Pause");
                if (isDownloading) {
                    DEBUG("Pause enter");
                    isDownloading = false;
                    downloadUtil.pauseDownload(this);
                }
                break;
            case R.id.btn_stop:
                DEBUG("Stop");
                downloadUtil.stopDownload(this);
                isDownloading = false;
                break;
            default:
                break;
        }
    }

    private void DEBUG(String info) {
        Log.d(LOG_TAG, info);
    }
}
